//
//  WelcomShowView.m
//  ZhenPin
//
//  Created by fei on 14-8-21.
//  Copyright (c) 2014年 zhenpin. All rights reserved.
//

#import "WelcomShowView.h"

@implementation WelcomShowView
#define WelcomImageView_tag 1001

static UIWindow *backWindow;
+(void)showWelcomView {
    if (backWindow == nil) {
        backWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        backWindow.backgroundColor = [UIColor clearColor];
        backWindow.userInteractionEnabled = YES;
        backWindow.windowLevel = UIWindowLevelStatusBar+1; //增加window级别
        backWindow.rootViewController = [[UIViewController alloc] init];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:backWindow.frame];
        imageView.tag = WelcomImageView_tag;
        imageView.image = [self getDefaultImage];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [backWindow.rootViewController.view addSubview:imageView];
        
    }
    [self getWelcomImageFormNet];
    [backWindow setHidden:NO];// 默认YES
}

+(void)addWelcomImage:(UIImage *)welcomImage {
    if (backWindow) {
        UIImageView *imageView = (UIImageView *)[backWindow.rootViewController.view viewWithTag:WelcomImageView_tag];
        imageView.image = welcomImage;
    } else {
    }
}

+(void)hide:(BOOL)animated {
    
    if (animated) {
        [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
            backWindow.transform = CGAffineTransformMakeScale(1.5, 1.5);
            backWindow.alpha = 0;
        }completion:^(BOOL finished){
            [self windowFadeOut];
        }];
    } else {
        [self windowFadeOut];
    }
}

+(void)windowFadeOut {
    UIImageView *imageView = (UIImageView *)[backWindow.rootViewController.view viewWithTag:WelcomImageView_tag];
    [imageView removeFromSuperview];
    backWindow.rootViewController = nil;
    [backWindow removeFromSuperview];
    backWindow = nil;
}

+(void)getWelcomImageFormNet {
    AFHTTPRequestOperationManager *httpManager = [AFHTTPRequestOperationManager manager];
    [httpManager GET:@"http://119.254.106.58:8380/zpapi/homePage/getHomeImg.json" parameters:nil success:^(AFHTTPRequestOperation *operation,id resultObject) {
        NSString *imageName = resultObject[@"iosImg"];
        if ([imageName isEqual:[NSNull null]] || imageName.length == 0) {
            
        } else {
            NSURL *imageUrl = [NSURL URLWithString:imageName];
            UIImage *splashImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
            [self addWelcomImage:splashImage];
        }
    }failure:^(AFHTTPRequestOperation *operation,NSError *error){
    }];
}

+(UIImage *)getDefaultImage {
    UIImage *image;
    if (isIphone5) {
        image = [UIImage imageNamed:@"startImage_1366"];
    } else {
        image = [UIImage imageNamed:@"startImage_960"];
    }
    
    return image;
}

@end
